﻿using DevExpress.Utils;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevMy.PublicSetUp;
using MDIDemo.Model;
using MDIDemo.PublicClass;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy.Vou
{
    public partial class Form_EquipmentParamer : Form_BaseAlone
    {
        public Form_EquipmentParamer(string FormText)
        {
            InitializeComponent();
            this.FormText = FormText;
        }
        private string FormText;
        private bool BeginChange = false;
        private WaitDialogForm waitDialogForm;
        private Class_MySqlDataBase class_MySqlDataBase;
        private Class_LookUpEdit class_LookUpEdit;

        private void SetColumn(bool IsDefault)
        {
            GridView gridView = this.GetGridPage().GetGridView();
            foreach (GridColumn gridColumn in gridView.Columns)
            {
                if (gridColumn.FieldName.Equals("defaultValue"))
                {
                    gridColumn.Visible = IsDefault;
                }
                if (gridColumn.FieldName.Equals("beginValue"))
                {
                    gridColumn.Visible = !IsDefault;
                }
                if (gridColumn.FieldName.Equals("endValue"))
                {
                    gridColumn.Visible = !IsDefault;
                }
            }
        }
        private void SetComponent(string FormText)
        {
            base.SetComponent(FormText, "I0023");
            base.SetGridDataSource(null);
            base.EnableViewButton(false);
            base.EnableOutButton(false);
            base.CloseAddButton();
            base.CloseDeleteButton();

            base.GetGridPage().PageSize = 50;

            LookUpEditColumnModel[] lookUpEditColumnModels = new LookUpEditColumnModel[2];
            lookUpEditColumnModels[0] = new LookUpEditColumnModel("equipmentName", "设备名称");
            lookUpEditColumnModels[1] = new LookUpEditColumnModel("id", "设备编号");

            Class_PublicMethod class_PublicMethod = new Class_PublicMethod();
            Class_DataBaseConDefault class_DataBaseConDefault = new Class_DataBaseConDefault();
            class_DataBaseConDefault = class_PublicMethod.FromXmlToDefaultValueObject<Class_DataBaseConDefault>("DataBaseDefaultValues");

            class_MySqlDataBase = new Class_MySqlDataBase(class_DataBaseConDefault.dataSourceUrl
                , class_DataBaseConDefault.dataBaseName
                , class_DataBaseConDefault.dataSourceUserName
                , class_DataBaseConDefault.dataSourcePassWord
                , class_DataBaseConDefault.Port);
            DataTable dataTable = new DataTable();
            dataTable = class_MySqlDataBase.GetDataTable(@"Select id
            , equipmentName
            From vou_equipment ORDER BY sortNo");
            class_LookUpEdit = new Class_LookUpEdit();
            class_LookUpEdit.SetLookUpEdit(this.lookUpEdit1, "equipmentName", "id", lookUpEditColumnModels, "设备名称");
            this.lookUpEdit1.Properties.DataSource = dataTable;
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                this.lookUpEdit1.ItemIndex = 0;
            }
            dataTable.Dispose();
            SetColumn(true);
            BeginChange = true;
        }
        public override int EditData(object id)
        {
            int index = -1;
            if (this.radioGroup1.SelectedIndex == 0)
            {
                Form_SetParamDefaultValue form_SetParamDefaultValue = new Form_SetParamDefaultValue();
                form_SetParamDefaultValue.SetWinType(0);
                form_SetParamDefaultValue.id = id.ToString();
                form_SetParamDefaultValue.Ini();
                form_SetParamDefaultValue.Text = this.Text;
                if (form_SetParamDefaultValue.ShowDialog() == DialogResult.OK)
                {
                    string Sql = string.Format(@"Update vou_equipmentparamer
                        Set defaultValue={1} Where id={0}"
                            , form_SetParamDefaultValue.paramDefaultMaxMinModel.id
                            , form_SetParamDefaultValue.paramDefaultMaxMinModel.defaultValue
                        );
                    index = class_MySqlDataBase.ExecuteSql(Sql);
                }
                form_SetParamDefaultValue.Dispose();
            }
            else
            {
                Form_SetParamMaxMinValue form_SetParamMaxMinValue = new Form_SetParamMaxMinValue();
                form_SetParamMaxMinValue.SetWinType(0);
                form_SetParamMaxMinValue.id = id.ToString();
                form_SetParamMaxMinValue.Ini();
                form_SetParamMaxMinValue.Text = this.Text;
                if (form_SetParamMaxMinValue.ShowDialog() == DialogResult.OK)
                {
                    string Sql = string.Format(@"Update vou_equipmentparamer
                        Set beginValue={1},endValue={2} Where id={0}"
                            , form_SetParamMaxMinValue.paramDefaultMaxMinModel.id
                            , form_SetParamMaxMinValue.paramDefaultMaxMinModel.MinValue
                            , form_SetParamMaxMinValue.paramDefaultMaxMinModel.MaxValue
                        );
                    index = class_MySqlDataBase.ExecuteSql(Sql);
                }
                form_SetParamMaxMinValue.Dispose();
            }
            return index;
        }
        public override void ViewData(object id)
        {
            MessageBox.Show(id.ToString());
        }

        public override void Finder(int PageNo)
        {
            if (this.lookUpEdit1.ItemIndex > -1)
            {
                waitDialogForm = new WaitDialogForm("正在玩命加载中......", "温馨提示");
                string WhereString = null;
                string mainId = this.lookUpEdit1.EditValue.ToString();
                string paramType = null;
                if (this.radioGroup1.SelectedIndex > -1)
                {
                    paramType = this.radioGroup1.Properties.Items[this.radioGroup1.SelectedIndex].Description;
                }
                WhereString = string.Format(" Where sensorNo={0}", (radioGroup2.SelectedIndex + 1).ToString());
                if (mainId != null)
                {
                    WhereString += string.Format(" And mainId = '{0}'"
                        , mainId
                        );
                }
                if (paramType != null)
                {
                    WhereString += " And paramType = '" + paramType + "'";
                }
                DataSet dataSet = this.GetFinder(PageNo, base.GetGridPage().PageSize, WhereString);
                base.SetGridDataSource(dataSet.Tables[0]);
                base.GetGridPage().SetRowCount(int.Parse(dataSet.Tables[1].Rows[0][0].ToString()));
                waitDialogForm.Close();
            }
        }
        private DataSet GetFinder(int PageNo, int PageSize, string Address)
        {
            string SelectSql = @"Select id
            , mainId
            , paramName
            , chinaName
            , sortNo
            , paramType
            , defaultValue
            , beginValue
            , endValue
            , valueType
            , sensorNo";
            string FormSql = "FROM vou_equipmentparamer";
            string WhereSql = Address;
            string GroupSql = null;
            string HavingSql = null;
            string OrderSql = "ORDER BY sensorNo,chinaName,sortNo";
            string TotalFieldName = "*";

            PageSqlModel pageSqlModel = class_MySqlDataBase.GetSql(SelectSql
            , FormSql
            , WhereSql
            , GroupSql
            , HavingSql
            , OrderSql
            , PageNo
            , PageSize
            , TotalFieldName);

            return class_MySqlDataBase.GetPageDataSet(pageSqlModel);
        }

        private void textEdit1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                Finder(base.GetGridPage().GetPageNo());
            }
        }

        private void Form_Eqiupment_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (waitDialogForm != null)
            {
                waitDialogForm.Dispose();
            }
            class_MySqlDataBase.CloseSqlConnection();
        }

        private void Form_EquipmentParamer_Load(object sender, EventArgs e)
        {
            SetComponent(FormText);
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetColumn(radioGroup1.SelectedIndex > 0 ? false : true);
            Finder(this.GetGridPage().GetPageNo());
        }

        private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            Finder(this.GetGridPage().GetPageNo());
        }

        private void radioGroup2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.BeginChange)
            {
                Finder(this.GetGridPage().GetPageNo());
            }
        }
    }
}
